package com.lanlian.cayman.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.lanlian.commonlib.base.BaseActivity;
import com.lanlian.cayman.R;
import com.lanlian.utillib.DisplayUtil;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.circlenavigator.CircleNavigator;

import java.util.ArrayList;
import java.util.List;

public class GuideActivity extends BaseActivity implements View.OnClickListener {

    private ViewPager mViewPager;
    private TextView mTvJump;
    private MagicIndicator mMagicIndicator;
    private TextView mTvInto;

    private List<ImageView> pagers;
    private int[] images = {R.mipmap.guide_one, R.mipmap.guide_two, R.mipmap.guide_three};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);
        initView();
        setListeners();
        initViewPagers();
        setRadio();
        mViewPager.setAdapter(new ImagePagerAdapter());
    }

    @Override
    protected void initView() {
        super.initView();
        mViewPager = findViewById(R.id.view_pager);
        mTvJump = findViewById(R.id.tv_jump);
        mMagicIndicator = findViewById(R.id.magic_indicator);
        mTvInto = findViewById(R.id.tv_into);
    }

    @Override
    protected void setListeners() {
        super.setListeners();
        mTvJump.setOnClickListener(this);
        mTvInto.setOnClickListener(this);
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {
            }

            @Override
            public void onPageSelected(int position) {
                if (position == pagers.size() - 1) {
                    mTvInto.setVisibility(View.VISIBLE);
                    mTvJump.setVisibility(View.GONE);
                    mMagicIndicator.setVisibility(View.GONE);
                } else {
                    mTvInto.setVisibility(View.GONE);
                    mTvJump.setVisibility(View.VISIBLE);
                    mMagicIndicator.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int i) {
            }
        });
    }

    private void initViewPagers() {
        pagers = new ArrayList<>();
        ImageView imageView;
        for (int image : images) {
            imageView = new ImageView(this);
            imageView.setImageResource(image);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            pagers.add(imageView);
        }
    }

    private class ImagePagerAdapter extends PagerAdapter {
        @Override
        public int getCount() {
            return pagers.size();
        }

        @NonNull
        @Override
        public Object instantiateItem(@NonNull ViewGroup container, int position) {
            container.addView(pagers.get(position));
            return pagers.get(position);
        }

        @Override
        public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
            container.removeView(pagers.get(position));
        }

        @Override
        public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
            return view == o;
        }

    }

    private void setRadio() {
        CircleNavigator circleNavigator = new CircleNavigator(this);
        circleNavigator.setCircleCount(images.length);
        circleNavigator.setCircleSpacing(DisplayUtil.dip2px(this, 25));
        circleNavigator.setRadius(DisplayUtil.dip2px(this, 5));
        circleNavigator.setCircleColor(getResources().getColor(R.color.color_6385CF));
        circleNavigator.setCircleClickListener(new CircleNavigator.OnCircleClickListener() {
            @Override
            public void onClick(int index) {
                mViewPager.setCurrentItem(index);
            }
        });
        mMagicIndicator.setNavigator(circleNavigator);
        ViewPagerHelper.bind(mMagicIndicator, mViewPager);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_jump:
                startActivity(new Intent(this, MainActivity.class));
                finish();
                break;
            case R.id.tv_into:
                startActivity(new Intent(this, MainActivity.class));
                finish();
                break;
            default:
                break;
        }
    }

}